﻿function AzureOnlineBlobsListView(context) {

    var self = this;

    var spinner = new SpinnerModel();

    var container = context.blobs.container;

    var blobs = context.blobs.blobs;

    var details = new AzureOnlineCreateBlobDetailsView(context);

    var pager = new PagerModel(blobs);

    var blobSettings = ko.computed(function () {
        var as = context.accountSettings();
        return as.folder('blobs').folder(container());
    });

    var filter = ko.observable('');
    var appliedFilter = ko.observable('');

    var filters = ko.computed(function () {
        return new MRUList(blobSettings(), 'filters');
    });
    
    filters.subscribe(function () {
        var f = filters().firstItem() || '';
        filter(f);
        appliedFilter(f);
    });
    
    function applyFilter() {
        var f = filter();
        appliedFilter(f);
        reload();

        var containerName = container();
        if (containerName) {
            filters().addItem(f);
        }
    }

    var spaRuntime = context.spaRuntime;

    var blobDetailsRoute = spaRuntime.findRouteByName('blobContainerDetails');

    var pageBlobViewerRoute = spaRuntime.findRouteByName('pageBlobViewer');

    function reload() {
        var cnt = container();
        if (!cnt) {
            blobs([]);
            return;
        }

        var api = context.api();
        spinner.enter();
        api.blob.listBlobs({
            container: cnt,
            prefix: appliedFilter(),
            success: function (raw) {
                spinner.leave();
                var res = [];
                var $doc = $(raw);

                var $items = $doc.find('Blobs Blob');
                $items.each(function () {
                    res.push(new AzureOnlineBlob($(this)));
                });

                blobs(res);
            },
            error: function () {
                spinner.leave();
                blobs([]);
                return 'notify';
            }
        });
    }

    function getDownloadUrl(blob) {
        var api = context.api();
        return api.blob.getDownloadBlobUrl({
            container: container(),
            blob: blob.name()
        });
    }

    function getViewUrl(blob) {
        var api = context.api();
        var blobName = blob.name();
        var type = blob.type();
        switch (type) {
            case 'BlockBlob':
                return api.blob.getDownloadBlobUrl({
                    container: container(),
                    blob: blobName
                }) + '&view=true&mimeType=text/plain';
            case 'PageBlob':
                return pageBlobViewerRoute.formatHash({
                    containerName: container(),
                    blobName: blobName
                });
            default:
                return null;
        }
    }

    function lockBlob(blob) {
        var api = context.api();
        api.blob.leaseBlob({
            container: container(),
            blob: blob.name(),
            action: 'acquire',
            success: function () {
                blob.locked(true);
            }
        });
    }

    function unlockBlob(blob) {
        var api = context.api();
        api.blob.leaseBlob({
            container: container(),
            blob: blob.name(),
            action: 'break',
            success: function () {
                blob.locked(false);
            }
        });
    }

    container.subscribe(function () {
        var api = context.api();
        if (api) {
            var cnt = container();
            reload(cnt);
        }
    });

    self.getBlobContainerDetailsUrl = function (blob) {
        return blobDetailsRoute.formatHash({
            blobName: blob.name()
        });
    };

    self.deleteBlob = function (blob) {
        var api = context.api();
        api.blob.deleteBlob({
            container: container(),
            blob: blob.name(),
            success: function () {
                blobs.remove(blob);
            }
        });
    };

    details.addOnSuccess(reload);

    self.lockBlob = lockBlob;
    self.unlockBlob = unlockBlob;

    self.getDownloadUrl = getDownloadUrl;
    self.getViewUrl = getViewUrl;

    self.spinner = spinner;
    self.reload = reload;
    self.container = container;
    self.blobs = blobs;
    self.pager = pager;
    self.details = details;

    self.filters = filters;
    self.filter = filter;
    self.applyFilter = applyFilter;
}